package com.ruoyi.kis.controller;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult_KIS;
import com.ruoyi.kis.Vars.Constants;
import com.ruoyi.kis.annotaion.BasicAuthCheck;
import com.ruoyi.kis.domain.PdResultF;
import com.ruoyi.kis.service.IKisService;
import com.ruoyi.kis.util.HttpSendWmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author: zhanglm
 * @CreateDate: 2025-03-03 15:35
 * @FullNameClass: com.ruoyi.kis.controller.KisPdResultController
 * @Description: 盘点结果单、盘点结果单反馈
 */
@RestController
@RequestMapping("/kispd")
@Component("kispd")
@Api("kis接口")
public class KisPdResultController extends BaseController {
    @Autowired
    private IKisService kisService;

    private static final Logger LOGGER = LoggerFactory.getLogger(KisPdResultController.class);
    /**
     * 盘点单下发
     * ERP > WMS
     *
     */
    @ApiOperation("盘点单下发")
    @RequestMapping("/pd")
    public AjaxResult_KIS pd(){

        //查询盘点单 TODO WMS未定义 盘点单 下发接口
        List<PdResultF> pdFList = kisService.queryPd();
        String s  = "";
        if (pdFList != null && pdFList.size() > 0){
            s = JSONUtil.parse(pdFList.get(0)).toString();
            LOGGER.info("盘点单下发数据："+s);
            try {
//                String msg = HttpUtil.post(Constants.PDD_URL,s);
                String msg = HttpSendWmsUtil.sendWms(Constants.PDD_URL,s);
                JSONObject entries = JSONUtil.parseObj(msg);

                if(entries != null ){
                    String msgcode = entries.get("msgcode").toString();
                    String kisFlag;
                    if (msgcode.equals("0")){
                        kisFlag = Constants.ZHIKU.KISFLAG_SUCCESS;

                    }else {
                        kisFlag = Constants.ZHIKU.KISFLAG_FAILURE;
                    }
                    int i = kisService.updateKisICStockBillFlag(pdFList.get(0).getFInterID(), kisFlag);
                    LOGGER.info("盘点单:{}下发反馈更新条数：{}，状态:{},反馈：{}",pdFList.get(0).getFBillNo(),i,kisFlag,msg);
                }
            }catch (HttpException e){
                LOGGER.error("盘点单下发反馈失败" + e.getMessage());
            }
        }
        return AjaxResult_KIS.success("OK",pdFList);
    }
    /**
     * 盘点单反馈
     */
    @ApiOperation("盘点反馈")
    @RequestMapping("/pdfeedback")
    @BasicAuthCheck
    public AjaxResult_KIS pdFeedback(@RequestBody PdResultF pdResultF){
        Map map = kisService.savePdStore(pdResultF);//盘赢盘亏入库单保存
        LOGGER.info("盘点单反馈："+map);
//        JSON parse = JSONUtil.parse(pdResultF);
//        String s =  parse.toString();
        if (map.get("MSGCODE").toString().equals("0")){
            JSON parse = JSONUtil.parse(pdResultF);
            String s =  parse.toString();
            LOGGER.info("盘点单反馈保存数据："+s);
            return AjaxResult_KIS.success("success","盘点单反馈保存成功");
        }else {
            LOGGER.error("盘点单反馈保存表体数量为0");
            return AjaxResult_KIS.success("error",map.get("MSG").toString());
        }
//        return AjaxResult_KIS.success("success","");
    }
}
